﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitBase.WinForm;

namespace ShowGridNumber
{
	public class ShowGridApplication : IExternalApplication
	{
		public bool DrawGridNumber { get; set; }

		public Result OnStartup(UIControlledApplication application)
		{
			this.m_version = Convert.ToInt32(application.ControlledApplication.VersionNumber);
			ShowGridApplication.m_ThisApp = this;
			ShowGridApplication.m_gridNumberShowForm = new GridNumberForm(ShowGridApplication.m_ThisApp);
			application.ViewActivated += this.OnRefreshGridsAndViewInfo;
			application.Idling += this.OnIdlingEvent;
			this.ReadShowState();
			if (this.m_hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				this.m_hRevitHandle = currentProcess.MainWindowHandle;
				this.m_hWndRevit = new WindowHandle(this.m_hRevitHandle);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result OnShutdown(UIControlledApplication application)
		{
			application.ViewActivated -= this.OnRefreshGridsAndViewInfo;
			application.Idling -= this.OnIdlingEvent;
			ShowGridApplication.m_gridNumberShowForm.Close();
			this.SaveShowState();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void OnRefreshGridsAndViewInfo(object sender, ViewActivatedEventArgs ViewActivated)
		{
			UIApplication uiapplication = sender as UIApplication;
			if (uiapplication == null)
			{
				return;
			}
			ShowGridApplication.m_gridNumberShowForm.UnLoad();
			this.m_uiDoc = uiapplication.ActiveUIDocument;
			if (!this.DrawGridNumber)
			{
				ShowGridApplication.m_gridNumberShowForm.Hide();
			}
			else if (!ShowGridApplication.m_gridNumberShowForm.Visible)
			{
				ShowGridApplication.m_gridNumberShowForm.Show(this.m_hWndRevit);
			}
			if (this.m_version < 2019)
			{
				ShowGridApplication.m_gridNumberShowForm.RefreshDocAndView(this.m_uiDoc);
				return;
			}
			ShowGridApplication.m_gridNumberShowForm.RefreshDocAndView2019(this.m_uiDoc);
		}

		private void OnIdlingEvent(object sender, IdlingEventArgs idlingArgs)
		{
			if (this.m_uiDoc == null)
			{
				return;
			}
			ShowGridApplication.m_gridNumberShowForm.DrawGridNumText(true);
		}

		public void ShowHideGridNumber()
		{
			if (this.DrawGridNumber)
			{
				YJKMessageBox.Show("全局轴号已关闭");
				ShowGridApplication.m_gridNumberShowForm.Hide();
			}
			else
			{
				ShowGridLevelNumOKForm showGridLevelNumOKForm = new ShowGridLevelNumOKForm();
				showGridLevelNumOKForm.ShowDialog();
				ShowGridApplication.m_gridNumberShowForm.SetShowLinkDocGridNum(showGridLevelNumOKForm.chkShowLink.Checked);
				if (this.m_version < 2019)
				{
					ShowGridApplication.m_gridNumberShowForm.Show(this.m_hWndRevit);
				}
				else
				{
					if (ShowGridApplication.m_gridNumberShowForm.Visible)
					{
						ShowGridApplication.m_gridNumberShowForm.Visible = false;
					}
					ShowGridApplication.m_gridNumberShowForm.Show(this.m_hWndRevit);
				}
				ShowGridApplication.m_gridNumberShowForm.UnLoad();
				if (this.m_version < 2019)
				{
					ShowGridApplication.m_gridNumberShowForm.RefreshDocAndView(this.m_uiDoc);
				}
				else
				{
					ShowGridApplication.m_gridNumberShowForm.RefreshDocAndView2019(this.m_uiDoc);
				}
			}
			this.DrawGridNumber = !this.DrawGridNumber;
		}

		private void SaveShowState()
		{
			FileStream fileStream = new FileStream(Product.UserDataLocation + "\\ShowGridNumber.txt", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			if (this.DrawGridNumber)
			{
				streamWriter.WriteLine("是");
			}
			else
			{
				streamWriter.WriteLine("否");
			}
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadShowState()
		{
			string path = Product.UserDataLocation + "\\ShowGridNumber.txt";
			if (!File.Exists(path))
			{
				this.DrawGridNumber = false;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			if (streamReader.ReadLine() == "是")
			{
				this.DrawGridNumber = true;
			}
			else
			{
				this.DrawGridNumber = false;
			}
			streamReader.Close();
			fileStream.Close();
		}

		public int m_version = 2016;

		public static ShowGridApplication m_ThisApp;

		public static GridNumberForm m_gridNumberShowForm;

		public IntPtr m_hRevitHandle = IntPtr.Zero;

		private UIDocument m_uiDoc;

		private WindowHandle m_hWndRevit;

		public enum WindowMessage
		{
			WM_DESTROY = 2,
			WM_SIZE = 5,
			WM_ERASEBKGND = 20,
			WM_KEYDOWN = 256,
			WM_KILLFOCUS = 8,
			WM_LBUTTONDBLCLK = 515,
			WM_LBUTTONDOWN = 513,
			WM_LBUTTONUP,
			WM_MOUSEWHEEL = 522,
			WM_MOUSEMOVE = 512,
			WM_PAINT = 15,
			WM_RBUTTONDBLCLK = 518,
			WM_RBUTTONDOWN = 516,
			WM_RBUTTONUP,
			WM_SETFOCUS = 7,
			WM_SETCURSOR = 32,
			WM_WINDOWPOSCHANGING = 70,
			WM_WINDOWPOSCHANGED,
			WM_MDICREATE = 544,
			WM_MDIDESTROY,
			WM_MDIACTIVATE,
			WM_MDIRESTORE,
			WM_MDINEXT,
			WM_MDIMAXIMIZE,
			WM_MDITILE,
			WM_MDICASCADE,
			WM_MDIICONARRANGE,
			WM_MDIGETACTIVE,
			WM_MDISETMENU = 560,
			WM_NCACTIVATE = 134
		}
	}
}
